{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca7c3f1b-3aca-46c4-a11d-ebc7ff0484a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#构造torch的dataloader\n",
    "import torch\n",
    "from torch.utils.data import Dataset\n",
    "import numpy as np\n",
    "import random\n",
    "from PIL import Image\n",
    "from torchvision import transforms\n",
    "import torchvision.transforms.functional as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7755e586-9c63-4989-8f01-ec5df86146cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyDataset(Dataset):\n",
    "    def __init__(self,filename,transform):\n",
    "        self.name=filename\n",
    "        self.frame_list = list()\n",
    "        self.transform=transform\n",
    "        with open(self.name) as split_f:\n",
    "            data = split_f.readlines()\n",
    "            for line in data:\n",
    "                self.frame_list.append(line)\n",
    "        self.len = len(self.frame_list)\n",
    "\n",
    "    def getframe(self,index):\n",
    "        data = self.frame_list[index]\n",
    "        data_info = data.split(',')\n",
    "        path = data_info[0]\n",
    "        start = int(data_info[1])\n",
    "        end = int(data_info[2])\n",
    "\n",
    "        x_data = []\n",
    "        fz = random.random()\n",
    "        for i in range(start, end):\n",
    "            if i == start:\n",
    "                x_data1 = Image.open(path + '/%04d.png' % i).convert('RGB')\n",
    "                x_data1 = transforms.Resize([224,224])(x_data1)\n",
    "                params = transforms.RandomCrop.get_params(x_data1,(224,224))\n",
    "                if fz > 0.5:\n",
    "                    x_data1 = tf.hflip(x_data1)\n",
    "                else:\n",
    "                    x_data1 = tf.vflip(x_data1)\n",
    "                x_data1 = x_data1.crop((params[0],params[1],params[0]+params[2],params[1]+params[3]))\n",
    "            else:\n",
    "                x_data1 = Image.open(path + '/%04d.png' % i).convert('RGB')\n",
    "                x_data1 = transforms.Resize([224,224])(x_data1)\n",
    "                if fz > 0.5:\n",
    "                    x_data1 = tf.hflip(x_data1)\n",
    "                else:\n",
    "                    x_data1 = tf.vflip(x_data1)\n",
    "                x_data1 = x_data1.crop((params[0],params[1],params[0]+params[2],params[1]+params[3]))\n",
    "            if self.transform != None: x_data.append(self.transform(x_data1).unsqueeze(0))\n",
    "        x_data_f = torch.cat(x_data, 0)\n",
    "        return x_data_f\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        data=self.frame_list[index]\n",
    "        data_info = data.split(',')\n",
    "        y_data=np.array([int(data_info[3])])\n",
    "        self.x_data=self.getframe(index)\n",
    "        self.y_data = torch.from_numpy(y_data).long()\n",
    "        return self.x_data,self.y_data\n",
    "    def __len__(self):\n",
    "        return self.len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91fa898c-5a68-440e-8e2a-56840f7ce694",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyDatasett(Dataset):\n",
    "    def __init__(self,filename,transform):\n",
    "        self.name=filename\n",
    "        self.frame_list = list()\n",
    "        self.transform=transform\n",
    "        with open(self.name) as split_f:\n",
    "            data = split_f.readlines()\n",
    "            for line in data:\n",
    "                self.frame_list.append(line)\n",
    "        self.len = len(self.frame_list)\n",
    "\n",
    "    def getframe(self,index):\n",
    "        data = self.frame_list[index]\n",
    "        data_info = data.split(',')\n",
    "        path = data_info[0]\n",
    "        start = int(data_info[1])\n",
    "        end = int(data_info[2])\n",
    "\n",
    "        x_data = []\n",
    "        for i in range(start, end):\n",
    "            if i == start:\n",
    "                x_data1 = Image.open(path + '/%04d.png' % i).convert('RGB')\n",
    "                x_data1 = transforms.Resize([224,224])(x_data1)\n",
    "            else:\n",
    "                x_data1 = Image.open(path + '/%04d.png' % i).convert('RGB')\n",
    "                x_data1 = transforms.Resize([224,224])(x_data1)\n",
    "            if self.transform != None: x_data.append(self.transform(x_data1).unsqueeze(0))\n",
    "        x_data_f = torch.cat(x_data, 0)\n",
    "        return x_data_f\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        data=self.frame_list[index]\n",
    "        data_info = data.split(',')\n",
    "        y_data=np.array([int(data_info[3])])\n",
    "        self.x_data=self.getframe(index)\n",
    "        self.y_data = torch.from_numpy(y_data).long()\n",
    "        return self.x_data,self.y_data\n",
    "    def __len__(self):\n",
    "        return self.len\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
